package org.brisling.ethic.service;

import java.util.List;

import org.brisling.base.domain.ListDomain;
import org.brisling.base.domain.RetDomain;
import org.brisling.common.ConstSysParam;
import org.brisling.common.util.JsonUtil;
import org.brisling.common.util.StringUtil;
import org.brisling.ethic.domain.EthicEvaluate;
import org.brisling.ethic.queryService.QueryEthicEvaluateService;
import org.brisling.ethic.repository.EthicEvaluateRepo;
import org.brisling.ethic.service.base.BaseEthicService;
import org.brisling.sysman.domain.SysLob;
import org.brisling.sysman.service.LobService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/deptEthicEvaluate")
/**
 * 用戶后台服務service
 * @Author : jackson wang
 * @Date   : 2015年6月8日 下午4:18:01
 * @version: 1.0
 */
public class DeptEthicEvaluateService extends BaseEthicService<EthicEvaluate>{
	
			
	@SuppressWarnings("unused")
	@Autowired
	private EthicEvaluateRepo baseRepo;	
	
//	@Autowired
//	private ShiroSecurityService shiroSecurityService;
	@Autowired
	private QueryEthicEvaluateService qryEthicEvaluateService;
//	@Autowired
//	private EthicEvaluateSubService ethicEvaluateSubService;
	@Autowired
	private LobService lobService;
	
	public DeptEthicEvaluateService(){
		this.moduleName = ConstSysParam.MODULE_ETHIC_EVALUATE;
		this.clazz = EthicEvaluate.class;
	}
	
	/**
	 * 获取业务主页面 
	 * @return
	 */
	@RequestMapping("/mainPage.action")
	public ModelAndView getMainPage(){
		return new ModelAndView("/ethic/dept_ethic_evaluate/main");
	}
	
	/**
	 * 获取业务编辑页面
	 * @return
	 */
	@RequestMapping("/editPage.action")
	public ModelAndView getEditPage(){
		return new ModelAndView("/ethic/dept_ethic_evaluate/editDlg");
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getListDomain.action")
	public ListDomain<EthicEvaluate> getRoleListDomain(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
				
		return super.getPagedList(page, rows);
			
	}
	
	/**
	 * 分页查询用户列表页面
	 * @param deptid
	 * @param page
	 * @param rows
	 * @param sort
	 * @return
	 */
	@RequestMapping("/getList.action")
	public List<EthicEvaluate> getRoleList(
			@RequestParam(value="page",required=false) String page,
			@RequestParam(value="rows",required=false) String rows,
			@RequestParam(value="sort",required=false) String sort){
		
		
			return super.getAll(page,rows,getSort(sort));
			
	}
	
	/**
	 * 根据角色名称查找角色集合
	 * @param roleNames
	 * @return
	 */
	public List<EthicEvaluate> getRoleListByRoleNames(List<String> roleNames){
		
		return super.getListByNames(roleNames);
			
	}
	
	/**
	 * 根据角色名称查找角色编码
	 * @param roleNames
	 * @return
	 */
	public List<String> getRoleIdsByRoleNames(List<String> roleNames){
		
		List<EthicEvaluate> roles = this.getRoleListByRoleNames(roleNames);
		
		
		return StringUtil.getStringIdList(roles);
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/del.action")
	public RetDomain delRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
				
		return super.delRows(id, ids);		 
	}
	
	/**
	 * 删除记录,单条或批量删除记录,id不为空，执行单条删除，id为空且ids不为空，执行批量删除
	 * @param id	待删除的单条记录id
	 * @param ids	待删除的多条记录id，以','分隔
	 * @return
	 */
	@RequestMapping("/flowDel.action")
	public RetDomain delFlowRow(@RequestParam(value="id",required=false) Long id,@RequestParam(value="ids",required=false) String ids){
		
			return delRow(id,ids);
		
	}
	
	
	/**
	 * 保存日常考评记录
	 * @param id
	 */
	@RequestMapping("/save.action")
	public EthicEvaluate save(@RequestBody String body){
		EthicEvaluate beforeSavedDomain = bodyToObj(body, EthicEvaluate.class);
		
		if(beforeSavedDomain!=null){
			
		}
		
		Long lobid = null;
		EthicEvaluate afterSavedDomain = null;
		if(beforeSavedDomain.getId()==null){
			//设置审核状态
			beforeSavedDomain.setStatus("0");
			afterSavedDomain = super.save(beforeSavedDomain);
			lobid = afterSavedDomain.getId();
		}else{
			afterSavedDomain = beforeSavedDomain;
			lobid = afterSavedDomain.getId();
		}
		
		beforeSavedDomain.setId(lobid);
		
		
		beforeSavedDomain.setLob_contents(beforeSavedDomain.getSublist());
			SysLob savedLob = lobService.save(beforeSavedDomain);
			String _lobid = Long.toString(savedLob.getId());
			afterSavedDomain.setLobid(_lobid);			
			
//			afterSavedDomain.setSublist(savedLob.getLob_contents());
		
		return super.save(afterSavedDomain);
	
	}
	
	/**
	 * 保存科评明细记录
	 * @param id
	 */
	@RequestMapping("/saveDeptDetail.action")
	public RetDomain saveDeptDetail(@RequestParam(value="id",required=false) Long id,
			@RequestParam(value="dept_evaluate_detail",required=false) String dept_detail,
			@RequestParam(value="dept_evaluate",required=false) String dept_evaluate,
			@RequestParam(value="dept_score",required=false) Double dept_score,
			@RequestBody String body){
		
		RetDomain ret = null;		
		
		if(id!=null ){			
			
			EthicEvaluate old = this.getOne(id);
			old.setDept_evaluate_detail(dept_detail);
			old.setDept_score(dept_score);
			old.setDept_evaluate(dept_evaluate);
			super.save(old);
			ret = RetDomain.successDomain("success");
		}
		
		ret = RetDomain.errorDomain("error");
			
//			afterSavedDomain.setSublist(savedLob.getLob_contents());
		
		return ret;
	
	}
	
	/**
	 * 批量保存
	 * @param body
	 * @return
	 */
	@RequestMapping("/saveList.action")
	public RetDomain saveList(@RequestBody String body){
		RetDomain domain = new RetDomain();
		if(body!=null && body.trim().length()>0){
			List<EthicEvaluate> lists = JsonUtil.bodyToListClass(body, EthicEvaluate.class);
			super.saveList(lists);
			domain.setSuccessMsg("success");
		}
		domain.setErrorMsg("error");
		
		return domain;
	}
	
	
	/**
	 * 单条记录审核
	 * @param id
	 * @param status
	 * @return
	 */
	@RequestMapping("/auditOne.action")
	public EthicEvaluate setAuditStatus(Long id,String status){
		String ret = null;
		if( (id!=null) && (status !=null)){
			ret =super.updateAuditStatus(id, status);
		}
		if(ret!=null && ret=="200"){
			return qryEthicEvaluateService.getOne(id);
		}
		return null;
	}
	
	/**
	 * 批量审核
	 * @param ids
	 * @param status
	 * @return
	 */
	@RequestMapping("/auditList.action")
	public RetDomain setAuditStatusList(@RequestParam(value="ids",required=false) String ids,@RequestParam(value="status",required=false)String status){
		
		RetDomain domain  = new RetDomain();
		if( (ids!=null) && (status !=null)){
			 int num = super.updateAuditStatusList(ids, status);
			 domain.setSuccessMsg(num+"");
		}else{
			 domain.setErrorMsg("error");
		}
		
		return domain;
	}
	
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/auditPermit.action")
	public EthicEvaluate auditPermit(@RequestParam(value="id",required=false) Long id){
		if(id!=null){
			return setAuditStatus(id,ConstSysParam.AUDIT_PERMIT);
		}
		return null;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/auditDenied.action")
	public EthicEvaluate auditDenied(@RequestParam(value="id",required=false) Long id){
		if(id!=null){
			return setAuditStatus(id,ConstSysParam.AUDIT_DENIED);
		}
		return null;
	}
	
	/**
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/auditSubmit.action")
	public EthicEvaluate auditRequest(@RequestParam(value="id",required=false) Long id){
		if(id!=null){
			return setAuditStatus(id,ConstSysParam.AUDIT_SUBMIT);
		}
		return null;
	}
	
	
}
